package puc.tcc.recomendaeventos;

import com.facebook.widget.ProfilePictureView;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class Menu extends Activity{
	
	private Button cadastroButton;
    private Button preferenciasButton;
    private Button eventosButton;
    private Button sairButton;
    private ProfilePictureView profilePictureView;
    private ImageView pictureView;
    private TextView greeting;
    private BancoUser dbUser;
    private int _ID;
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);  	
        
        setContentView(R.layout.menu);
        _ID = this.getIntent().getIntExtra("_ID", 0);
        profilePictureView = (ProfilePictureView) findViewById(R.id.profilePicture);
        pictureView = new ImageView(this);
        greeting = (TextView) findViewById(R.id.greeting);
        dbUser = new BancoUser(this);
        Cursor usuario = dbUser.buscar(Integer.toString(_ID));
		
		usuario.moveToFirst();
		int columnIndex = usuario.getColumnIndex("nome");
		String snome = usuario.getString(columnIndex);
		greeting.setText(getString(R.string.hello_user, snome));
        byte[] bfoto = usuario.getBlob(usuario.getColumnIndexOrThrow("foto"));
        if (bfoto!=null){
        	Bitmap bm = BitmapFactory.decodeByteArray(bfoto, 0, bfoto.length);
        	pictureView.setImageBitmap(bm);
            profilePictureView.addView(pictureView);
        }
        usuario.close();
        eventosButton = (Button) findViewById(R.id.btEventos);
        eventosButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                onClickEventos();
            }
        });

        preferenciasButton = (Button) findViewById(R.id.btPreferencias);
        preferenciasButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                onClickPreferencias();
            }
        });

        cadastroButton = (Button) findViewById(R.id.btCadastro);
        cadastroButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                onClickCadastro();
            }
        });
        
        sairButton = (Button) findViewById(R.id.btSair);
        sairButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                onClickSair();
            }
        });
    }

    

    private void onClickEventos() {
    	
    	startActivity(new Intent(this, ListaEventos.class));
    	
    }

    private void onClickCadastro() {
    	Intent it = new Intent(this, CadUser.class);
    	it.putExtra("_ID", _ID);
    	startActivity(it);
    	
    }

    private void onClickPreferencias() {
    	
    	startActivity(new Intent(this, Preferencias.class));
    	
    }
    
    public void back(){
    	startActivity(new Intent(this, MainActivity.class));
    }
    
    private void onClickSair() {
    	String title = "Sair";
    	String message = "Tem certeza que deseja sair?";
    	new AlertDialog.Builder(this)
        .setTitle(title)
        .setMessage(message)
        .setPositiveButton("Sair", new OnClickListener(){
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				finish();
				back();
				
			}
        })
        .setNegativeButton("Cancelar", null)
        .show();
    }        

}
